<template>
    <div class="app-container">
        <div style="display:flex;justify-content:space-between;align-items: center;">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
                <el-form-item label="房间名称" prop="name">
                    <el-input v-model="queryParams.name" placeholder="请输入房间名称" clearable size="small" style="width: 150px"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
                    <el-button size="small" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <div style="text-align:right;margin-bottom: 22px;">
                <el-button-group slot="reference">
                    <el-button size="small" icon="el-icon-refresh" @click="getList" />
                    <el-button size="small" icon="el-icon-plus" @click="handleAdd">新增</el-button>
                </el-button-group>
            </div>
        </div>

        <el-table v-loading="loading" border :data="roomList" @sort-change="sortChange">
            <el-table-column label="编号" prop="id" width="50" align="center" />
            <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true" />
            <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat"></el-table-column>
            <el-table-column label="设备状态" align="center" prop="onlineState"
                :formatter="gateOnlineStatusFormat"></el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
                    <el-button size="mini" type="text" icon="el-icon-user" @click="toGateAuth(scope.row)">人员</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <el-dialog v-dialogDrag class="form_dialog" :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="房间名称" prop="name">
                            <el-input v-model="form.name" placeholder="请输入房号/名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备编号" prop="device.serialNumber">
                            <el-input v-model="form.device.serialNumber" placeholder="向负责人索取" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="管理账号" prop="device.username">
                            <el-input v-model="form.device.username" placeholder="请输入管理账号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="管理密码" prop="device.password">
                            <el-input v-model="form.device.password" placeholder="请输入管理密码" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="门禁品牌" prop="device.factoryCode">
                            <el-radio-group v-model="form.device.factoryCode">
                                <el-radio v-for="dict in gateFactoryCodes" :key="dict.value" :label="dict.value">
                                    {{ dict.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="房间类型" prop="type">
                            <el-radio-group v-model="form.type">
                                <el-radio v-for="dict in roomTypes" :key="dict.value" :label="dict.value">
                                    {{ dict.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item label="房间容量" prop="capacity">
                            <el-input v-model="form.capacity" placeholder="房间容纳人数" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import {
    listRoom,
    saveRoom,
    getRoom,
    delRoom,
} from "@/api/gate/gate";
import { listSimpleMenus } from "@/api/system/menu";
import { assignRoleMenu, listRoleMenus, assignRoleDataScope } from "@/api/system/permission";
import { listSimpleDepts } from "@/api/system/dept";
import { SysCommonStatusEnum } from "@/utils/constants";
import { DICT_TYPE, getDictDataLabel, getDictDatas } from "@/utils/dict";
import { CONSTS, getRoomTypeLabel, getGateOnlineStatusLabel, getGateFactoryCodes, getRoomTypes } from "@/utils/const";

export default {
    name: "Role",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 房间表格数据
            roomList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNo: 1,
                pageSize: 20,
                name: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "角色名称不能为空", trigger: "blur" }
                ],
                type: [
                    { required: true, message: "房间类型不能为空", trigger: "blur" }
                ],
                device: {
                    serialNumber: [
                        { required: true, message: "设备编号不能为空", trigger: "blur" }
                    ],
                    username: [
                        { required: true, message: "管理账号不能为空", trigger: "blur" }
                    ],
                    password: [
                        { required: true, message: "管理密码不能为空", trigger: "blur" }
                    ],
                    factoryCode: [
                        { required: true, message: "门禁品牌不能为空", trigger: "blur" }
                    ],
                },
            },

            // 枚举
            SysCommonStatusEnum: SysCommonStatusEnum,
            // 数据字典
            //门禁品牌
            gateFactoryCodes: getGateFactoryCodes(),
            roomTypes: getRoomTypes(),
        };
    },
    created() {
        this.reset()
        this.getList();
    },
    methods: {
        /** 查询角色列表 */
        getList() {
            this.loading = true;
            listRoom(this.queryParams).then(
                response => {
                    this.roomList = response.data.list;
                    this.total = response.data.total;
                    this.loading = false;
                }
            );
        },
        sortChange(column, prop, order) {
            if (column.order === 'ascending') {
                this.queryParams.orderBy = column.prop + ' ' + 'asc'
            } else if (column.order === 'descending') {
                this.queryParams.orderBy = column.prop + ' ' + 'desc'
            } else {
                this.queryParams.orderBy = ''
            }
            this.getList()
        },
        // 房间类型翻译
        typeFormat(row, column) {
            return getRoomTypeLabel(row.type)
        },

        //门禁状态翻译
        gateOnlineStatusFormat(row, column) {
            return getGateOnlineStatusLabel(row.onlineState)
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },

        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                name: undefined,
                type: CONSTS.ROOM_TYPE_DEF,
                capacity: 1,
                device: {
                    serialNumber: undefined,
                    username: undefined,
                    password: undefined,
                    factoryCode: CONSTS.GATE_FACTORY_CODE_DAHUA,
                }
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNo = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "新增房间";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id
            getRoom(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改门禁";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    saveRoom(this.form).then(response => {
                        this.msgSuccess("保存成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id
            this.$confirm('是否确认删除房间编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delRoom(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
        //员工认证管理
        toGateAuth(row) {
            this.$router.push({
                path: '/auth/' + row.id
            });
        }
    }
};
</script>
  